package com.neouton.crm.payRecords.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neouton.common.util.AuthUtils;
import com.neouton.common.util.PageUtil;
import com.neouton.config.ResponseAPI;
import com.neouton.crm.entity.SysUserEntity;
import com.neouton.crm.payRecords.entity.PaymentRecords;
import com.neouton.crm.payRecords.entity.PaymentRequest;
import com.neouton.crm.payRecords.entity.PaymentTable;
import com.neouton.crm.payRecords.service.PaymentRecordsService;
import com.neouton.crm.pm.entity.SalesProjectManageVO;
import com.neouton.crm.pm.service.ProjectMemberService;
import com.neouton.crm.receiptRecords.entity.ReceiptRecords;
import com.neouton.crm.receiptRecords.entity.ReceiptTable;
import com.neouton.crm.sales.entity.SalesFileRelationVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * 付款记录
 *
 * @author
 * @date 2023-02-03 15:27:00
 * @Version 1.0
 */
@RestController
@Slf4j
@RequestMapping("/payRequest")
public class PaymentRequestController {

    @Resource
    private PaymentRecordsService paymentRecordsService;
    @Resource
    private ProjectMemberService projectMemberService;

    /**
     * 列表信息查询
     *
     * @param pageNum
     * @param pageSize
     * @param entity
     * @return
     */
    @PostMapping("/getPaymentRecords/{pageNum}/{pageSize}")
    public List<PaymentRecords> list(@PathVariable(value = "pageNum") int pageNum,
                                         @PathVariable(value = "pageSize") int pageSize,
                                         @RequestBody PaymentRecords entity) {
//        IPage<ManagerPayRecord> page = new Page<>(pageNum, pageSize, true);
        List<PaymentRecords> result = paymentRecordsService.queryPage( entity);
        return result;
    }

    /**
     * 数据保存
     *
     * @return
     */
    @PostMapping(value = "/savePaymentRequest")
    @ResponseBody
    public ResponseAPI savePaymentRequest(@RequestHeader("token") String token,@RequestBody PaymentRequest paymentRequest) {
        return ResponseAPI.successData(paymentRecordsService.savePaymentRequest(paymentRequest,token));
    }

    /**
     * 修改项目
     *
     * @param paymentRequest
     * @return
     */
    @PostMapping("/updatePayRequestAfterPass/")
    public ResponseAPI updatePayRequestAfterPass(@RequestBody PaymentRequest paymentRequest, @RequestHeader("token") String token) {
        paymentRecordsService.updatePayRequestAfterPass(paymentRequest,token);
        return  ResponseAPI.successData("success");
    }


    @PostMapping("/queryReceiptRecordFromCache/{payRequestId}")
    public ResponseAPI queryReceiptRecordFromCache(@PathVariable(value = "payRequestId") int payRequestId, @RequestHeader("token") String token) {
        PaymentRequest paymentRequest = paymentRecordsService.queryReceiptRecordFromCache(payRequestId, token);
        return ResponseAPI.successData(paymentRequest);
    }




    @PostMapping("/queryPayTablesByIdAndPlanPeriod")
    @ResponseBody
    public ResponseAPI queryPayTablesByIdAndPlanPeriod(@RequestBody PaymentTable entity) {
           List<PaymentTable> list = paymentRecordsService.queryPayTablesByIdAndPlanPeriod(entity);
           return ResponseAPI.successData(list);
    }

    /**
     * 付款记录：数据修改
     */
    @RequestMapping("/updatePaymentRequest")
    @ResponseBody
    public ResponseAPI update(@RequestBody PaymentRequest entity,
                              @RequestHeader(value = "token", required = false) Integer token) {
            paymentRecordsService.updatePaymentRequestById(entity);
        return ResponseAPI.successMes("修改模型数据成功");
    }

    /**
     * 查询付款申请
     * @param entity
     * @return
     */
    @PostMapping("/queryPayRequestMoney/")
    public ResponseAPI queryPayRequestMoney(@RequestBody PaymentRequest entity) {
        List<PaymentRequest> list =  paymentRecordsService.queryPayRequestMoney(entity);
        return ResponseAPI.successData(list);
    }

    /**
     * 付款申请审批通过
     * @param entity
     * @param token
     * @return
     */
    @PostMapping("/approvedPayRequest")
    public ResponseAPI approvedPayRequest(@RequestBody PaymentRequest entity,@RequestHeader("token") String token) {
        return ResponseAPI.successData(paymentRecordsService.approvedPayRequest(entity,token,false));
    }
    /**
     * 付款申请审批驳回
     * @param entity
     * @param token
     * @return
     */
    @PostMapping("/rejectionPayRequest")
    public ResponseAPI rejectionPayRequest(@RequestBody PaymentRequest entity,@RequestHeader("token") String token) {
        return ResponseAPI.successData(paymentRecordsService.rejectionPayRequest(entity,token));
    }
    /**
     * 根据审批id获取付款申请历史数据
     * @param approvalId
     * @return
     */
    @PostMapping("/queryPayRequestHistory/{approvalId}")
    public ResponseAPI queryPayRequestHistory(@PathVariable(value = "approvalId") Integer approvalId) {
        return ResponseAPI.successData(paymentRecordsService.queryPayRequestHistory(approvalId));
    }

    /**
     * 查询付款申请
     *
     * @param paymentRequest
     * @return
     */
    @PostMapping("/queryPaymentRequestListInfo/{pageNum}/{pageSize}")
    public ResponseAPI queryPaymentRequestListInfo(@PathVariable(value = "pageNum") Integer pageNum,
                                                                           @PathVariable(value = "pageSize") Integer pageSize,
                                                                           @RequestBody PaymentRequest paymentRequest,
                                                                           @RequestHeader("token") String token) {
        List<SysUserEntity> userList = projectMemberService.queryUserList(AuthUtils.getUserId(),"paymentRequest", token);
        IPage<PaymentRequest> page = new Page<>(pageNum, pageSize, true);
        PageUtil<PaymentRequest> list = paymentRecordsService.queryPaymentRequestListInfo(page, paymentRequest, userList, token);
        return ResponseAPI.successData(list);
    }

    /**
     * 查询付款申请
     *
     * @param paymentRequest
     * @return
     */
    @PostMapping("/queryToDoPaymentRequestListInfo/{pageNum}/{pageSize}")
    public ResponseAPI queryToDoPaymentRequestListInfo(@PathVariable(value = "pageNum") Integer pageNum,
                                                   @PathVariable(value = "pageSize") Integer pageSize,
                                                   @RequestBody PaymentRequest paymentRequest,
                                                   @RequestHeader("token") String token) {
        IPage<PaymentRequest> page = new Page<>(pageNum, pageSize, true);
        PageUtil<PaymentRequest> list = paymentRecordsService.queryToDoPaymentRequestListInfo(page, paymentRequest, token);
        return ResponseAPI.successData(list);
    }


    /**
     * 撤回或驳回修改付款计划和表格数据
     * @param approvalId
     * @return
     */
    @PostMapping("/updatePayedRecordTableData/{approvalId}")
    public ResponseAPI updatePayedRecordTableData(@PathVariable(value = "approvalId") Integer approvalId) {
        paymentRecordsService.updatePayedRecordTableData(approvalId);
        return ResponseAPI.success();
    }

    @PostMapping("/deletePayedRequestById/{payRequestId}")
    public ResponseAPI deletePayedRequestById(@PathVariable(value = "payRequestId") Integer payRequestId) {
        Boolean flag= paymentRecordsService.deletePayedRequestById(payRequestId);
        return ResponseAPI.successData(flag);
    }

    @PostMapping("/updatePrIsNotById/{payRequestId}")
    public ResponseAPI updatePrIsNotById(@PathVariable(value = "payRequestId") Integer payRequestId) {
         paymentRecordsService.updatePrIsNotById(payRequestId);
        return ResponseAPI.success();
    }

    /**
     * 导出Excel
     * @param response
     * @param entity
     * @throws Exception
     */
    @PostMapping("/exportInfo")
    public void exportInfo(HttpServletResponse response, @RequestBody PaymentRequest entity, @RequestHeader("token") String token) throws Exception{
        List<SysUserEntity> userList = projectMemberService.queryUserList(AuthUtils.getUserId(),"paymentRecords", token);
        List<PaymentRequest> list = paymentRecordsService.queryPaymentRequestListInfo(entity, userList, token);
        paymentRecordsService.exportPaymentRequestInfo(response,list,token);
    }
}
